package serverService;


import common.Message;
import common.MessageType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lhn
 * @date 2024-05-07 16:02
 */

public class ManageServerConnectClientThread {
    private static HashMap<String, ServerConnectClientThread> hashMap = new HashMap<>();
    //TODO  offlineDB   一个存放离线发送的消息，一个存放离线发送的文件
    private static ConcurrentHashMap<String, ArrayList<Message>> offlineMsgDB = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<String, ArrayList<Message>> offlineFileDB = new ConcurrentHashMap<>();
    public static HashMap<String, ServerConnectClientThread> getHashMap() {
        return hashMap;
    }

    public static void addServerConnectClientThread(String userId, ServerConnectClientThread serverConnectClientThread) {
        hashMap.put(userId, serverConnectClientThread);
    }
    public static ServerConnectClientThread getServerConnectClientThread(String userId ) {
        return hashMap.get(userId);
    }

    //返回在线用户列表
    public static String getOnlineUsers() {
        Iterator iterator = hashMap.keySet().iterator();
        String onlineUserList = "";
        while (iterator.hasNext()) {
            onlineUserList += iterator.next().toString() + " ";
        }
        return onlineUserList;
    }

    public static void removeServerConnectClientThread(String userId) {
        if (hashMap.get(userId)!=null) {
            hashMap.remove(userId);
        }
    }

    /**
    *
    * @author lihaonan
    * @date 2024/5/16 19:38
     * @param userId
     * @return boolean
     * 根据socket连接的线程查询用户是否在线
    */
    public static boolean checkUserOnline(String userId) {
        if (hashMap.get(userId) != null) {
            return true;
        }
        return false;
    }

    /**
    *
    * @author lihaonan
    * @date 2024/5/17 15:46
     * @param userId    接收消息者的id
     * @param message   消息
    */
    public static void addOfflineMsg(String userId, Message message) {
        //TODO  添加离线消息
        ArrayList<Message> messages = new ArrayList<>();
        message.setMesType(MessageType.MESSAGE_OFFLINE_MESSAGE);
        if (getOfflineMsgArr(userId) != null) {
            messages = getOfflineMsgArr(userId);
        }
        messages.add(message);
        offlineMsgDB.put(userId, messages);
    }

    /**
     * @param userId 消息接收者的id
     * @return
     * @author lihaonan
     * @date 2024/5/16 19:42
     */
    public static ArrayList<Message> getOfflineMsgArr(String userId) {
        if (checkOfflineMsgArr(userId)) {
            return offlineMsgDB.get(userId);
        } else {
            return null;
        }
    }

    /**
    *
    * @author lihaonan
    * @date 2024/5/16 19:45
     * @param userId 消息接收者的id
     * @return boolean 消息接收者的arr是否存在
    */
    public static boolean checkOfflineMsgArr(String userId) {
        return offlineMsgDB.get(userId) != null;
    }

    /**
    *
    * @author lihaonan
    * @date 2024/5/16 19:51
     * @param userId
     * 将对应的离线消息发送出去后，移除
    */
    public static void removeOfflineMsgArr(String userId) {
        if (checkOfflineMsgArr(userId)) {
            offlineMsgDB.remove(userId);
        }
    }

    /**
     *
     * @author lihaonan
     * @date 2024/5/17 15:46
     * @param userId    接收消息者的id
     * @param message   文件消息
     */
    public static void addOfflineFile(String userId, Message message) {
        //TODO  添加离线文件消息
        ArrayList<Message> messages = new ArrayList<>();
        message.setMesType(MessageType.MESSAGE_OFFLINE_FILE_MESSAGE);
        if (getOfflineFileArr(userId) != null) {
            messages = getOfflineFileArr(userId);
        }
        messages.add(message);
        offlineFileDB.put(userId, messages);

    }

    /**
     * @param userId 消息接收者的id
     * @return
     * @author lihaonan
     * @date 2024/5/16 19:42
     */
    public static ArrayList<Message> getOfflineFileArr(String userId) {
        if (checkOfflineFileArr(userId)) {
            return offlineFileDB.get(userId);
        } else {
            return null;
        }
    }

    /**
     *
     * @author lihaonan
     * @date 2024/5/16 19:45
     * @param userId 消息接收者的id
     * @return boolean 消息接收者的arr是否存在
     */
    public static boolean checkOfflineFileArr(String userId) {
        return offlineFileDB.get(userId) != null;
    }

    /**
     *
     * @author lihaonan
     * @date 2024/5/16 19:51
     * @param userId
     * 将对应的离线文件消息发送出去后，移除
     */
    public static void removeOfflineFileArr(String userId) {
        if (checkOfflineFileArr(userId)) {
            offlineFileDB.remove(userId);
        }
    }
}